#!/usr/bin/python

''' Class to generate time slices for the dynamic-nmf routines. '''

from datetime import datetime

class TimeSlices:
    def __init__(self, content, date_idx, date_format = '%d/%m/%Y'):
        self.content = content
        self.date_idx = date_idx
        self.date_format = date_format
    
    def sort_time(self):
        # Parse date
        content_to_process = self.content
        for row in content_to_process:
            # Add date time object to content
            row['date_time'] = datetime.strptime(row[self.date_idx], self.date_format)
        
        # Put content in chronological order
        content_sorted = sorted(content_to_process, key=lambda k: k['date_time']) 
        
        return content_sorted
    
    def get(self, text_id):
        # Get data sorted in chronological order
        content_sorted = self.sort_time()
        
        # Return list of time slices: [docid, text, slice]
        return [(row['docid'], row[text_id], row['date_time'].year) for row in content_sorted]

